package com.shujia;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;

public class RedisCluster {
    public static void main(String[] args) {
        //创建Jedis对象
//        Jedis jedis = new Jedis("192.168.220.100", 7003);
//        jedis.set("name","xiaohu");
        //如何通过javaAPI连接redis集群
        //创建JedisCluster的对象
        // public JedisCluster(Set<HostAndPort> nodes)
        //需要先将所有集群中的节点封装成一个Set集合
        //每一个节点需要先封装成HostAndPort对象
        HashSet<HostAndPort> hostAndPorts = new HashSet<>();
        // public HostAndPort(String host, int port)
        hostAndPorts.add(new HostAndPort("192.168.220.100",7000));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7001));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7002));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7003));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7004));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7005));
        hostAndPorts.add(new HostAndPort("192.168.220.100",7006));

        JedisCluster jedisCluster = new JedisCluster(hostAndPorts);

        jedisCluster.set("name","xiaohu");
        jedisCluster.set("age","18");
        String s = jedisCluster.get("name");
        System.out.println(s);
        String s2 = jedisCluster.get("age");
        System.out.println(s2);

        //如果两个键不在一个主节点中存储，不能一起删除
        //推荐集群模式下的redis，删除键的时候，一个一个删
        jedisCluster.del("name");
        jedisCluster.del("age");


        //关闭连接
        jedisCluster.close();


    }
}
